﻿

-- =============================================
-- Author:		<赵洪涛>
-- Create date: <2012-05-07>
-- Description:	<转移项目>
-- =============================================
CREATE PROCEDURE [dbo].[proc_Project_Shift] 
(
	@ExCompanyId int,
	@DeptId int,
	@UserId varchar(30),
	@UserName nvarchar(50),
	@ProjectId uniqueidentifier,
	@moveType int
)
AS
BEGIN
	Declare @ChildCount int,@OId nvarchar(100)

	Set @OId = Null
	Select @OId=OrderId From Project Where ProjectId=@ProjectId
	If @OId Is Null
	Begin
		Select -1 As ReturnValue	--该项目已经被删除
		Return
	End

	Set @ChildCount = 1
	Select @ChildCount=Count(0)	From Project_Task Where ProjectId=@ProjectId
	If @ChildCount > 0
	Begin
		Select 1 As ReturnValue	--该项目已经下发任务
		Return
	End

	If @moveType = 1	--转移到其他公司执行了(，初始化跟单客服)
	Begin
		Update Project Set MoveCompanyId=@ExCompanyId,MoveUserId=@UserId Where ProjectId=@ProjectId
		Update [Order] Set ExCompanyId=@ExCompanyId Where OId=@OId
--		Update [Order] Set ExCompanyId=@ExCompanyId,TrackKfDepId=Null,TrackKfAccount=Null,TrackKfName=Null Where OId=@OId
	End
	Else	--转移给跟进客服
		Update Project Set MoveCompanyId=@ExCompanyId,MoveUserId=@UserId Where ProjectId=@ProjectId
--		Update Project Set ExecDepartmentId=@DeptId,ExecPerson=@UserId,ExecPersonName=@UserName Where ProjectId=@ProjectId

	Select 2 As ReturnValue
END
